package ru.pb.dao.game.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import ru.pb.game.dao.CheckPlayerDAO;
import ru.pb.global.dao.annotations.DAO;
import ru.pb.global.dbc.DatabaseFactory;
import ru.pb.global.dbc.DatabaseUtils;

/**
 * @author: Felixx
 */
@DAO(database = "MySQL")
public class CheckPlayerDAOImpl implements CheckPlayerDAO
{
	private static final Logger _log = Logger.getLogger(CheckPlayerDAOImpl.class);

	@Override
	public int getObjectIdByName(String name)
	{
		int res = 0;

		Connection con = null;
		PreparedStatement statement = null;
		ResultSet rset = null;
		try
		{
			con = DatabaseFactory.getInstance().newConnection();
			statement = con.prepareStatement("SELECT player_id FROM players WHERE player_name=? LIMIT 1");
			statement.setString(1, name);
			rset = statement.executeQuery();

			if(rset.next())
				res = rset.getInt("player_id");
		}
		catch(SQLException e)
		{
			_log.info("SQLException: " + e, e);
		}
		finally
		{
			DatabaseUtils.closeDatabaseCSR(con, statement, rset);
		}
		return res;
	}
}